package com.smalls.redshoes.ui.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.TextView;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.mvp.bean.LiveChannel;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.AppUtils;
import com.smalls.redshoes.R;
import com.smalls.redshoes.adapter.ChannelAdapter;
import com.smalls.redshoes.util.Constant;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class LivePlayControlProgramsView extends AutoHideLayout implements OnItemClickListener, OnItemSelectedListener, OnKeyListener {
    private static String TAG = com.smalls.newvideotwo.utils.Constant.baddcoolio2tag? com.smalls.newvideotwo.utils.Constant.TAGPREFIXSTR + LivePlayControlProgramsView.class.getSimpleName():LivePlayControlProgramsView.class.getSimpleName();
    private ChannelAdapter mAdatper;
    private AnimationDrawable mAnimationDrawable;
    private TextView mBottomStateTv;
    private TextView mChannelNameTv;
    private ListView mDataLv;
    private ArrayList<LiveChannel> mDatas;
    private TextView mDateTimeTv;
    private View mLoadingLineView;
    private TextView mNetSpeedTv;
    private View mRootView;
    private TextView mSequenceTv;
    private OnLiveProgramItemListener onLiveProgramItemListener;

    public interface OnLiveProgramItemListener {
        void onLiveProgramItemClicked(AdapterView<?> adapterView, View view, int i, long j);
    }

    public LivePlayControlProgramsView(Context context) {
        this(context, null);
    }

    public LivePlayControlProgramsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LivePlayControlProgramsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected void init(Context context) {
        setAutoHide(false);
        super.init(context);

        this.mRootView = LayoutInflater.from(context).inflate(R.layout.rs_layout_live_control_view, this, true);

        this.mSequenceTv = (TextView) this.mRootView.findViewById(R.id.tv_bar_num);
        this.mChannelNameTv = (TextView) this.mRootView.findViewById(R.id.tv_bar_channelname);
        this.mDateTimeTv = (TextView) this.mRootView.findViewById(R.id.tv_time);
        this.mBottomStateTv = (TextView) this.mRootView.findViewById(R.id.tv_bar_loading);
        this.mNetSpeedTv = (TextView) this.mRootView.findViewById(R.id.tv_netspeed);
        this.mDataLv = (ListView) this.mRootView.findViewById(R.id.lv_live);
        this.mDataLv.setOnItemClickListener(this);
        this.mDataLv.setOnItemSelectedListener(this);
        this.mDataLv.setOnKeyListener(this);
        this.mAdatper = new ChannelAdapter(context);
        this.mDataLv.setAdapter(this.mAdatper);
        this.mDataLv.requestFocus();
        setTime();
    }

    public void setSateTextNull() {
        if (this.mBottomStateTv != null && !TextUtils.isEmpty(this.mBottomStateTv.getText())) {
            this.mBottomStateTv.setText("");
        }
    }

    public void startLoadingAnimation() {
        if (this.mAnimationDrawable != null) {
            this.mAnimationDrawable.start();
        }
    }

    public void stopLoadingAnimation() {
        if (this.mAnimationDrawable != null) {
            this.mAnimationDrawable.stop();
        }
    }

    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == 0 && this.mDataLv != null && this.mAdatper != null && this.mAdatper.getClickedPosition() != -1) {
            this.mDataLv.post(new Runnable() {
                public void run() {
                    LivePlayControlProgramsView.this.mDataLv.setSelection(LivePlayControlProgramsView.this.mAdatper.getClickedPosition());
                }
            });
        }
    }

    public void setTime() {
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
        if (this.mDateTimeTv != null && !TextUtils.isEmpty(time)) {
            this.mDateTimeTv.setText(time);
        }
    }

    private int getLastClickedRow() {
        return AppUtils.getConfigSharedPreferences(NewDTVApp.getApp()).getInt(Constant.KEY_LAST_CLICK_LIVE_PROGRAM_ROW, 0);
    }

    private void saveClickedRow(int row) {
        AppUtils.getConfigSharedPreferences(NewDTVApp.getApp()).edit().putInt(Constant.KEY_LAST_CLICK_LIVE_PROGRAM_ROW, row).commit();
    }

    public void setDatas(ArrayList<LiveChannel> mDatas) {
        LogUtil.d(TAG, "setDatas");
        this.mDatas = mDatas;
        if (this.mAdatper != null && mDatas != null) {
            this.mAdatper.setData(this.mDatas);
            if (this.mAdatper.getClickedPosition() == -1) {
                final int clickedPosition = getLastClickedRow();
                LogUtil.d(TAG, "clickedPosition=" + clickedPosition);
                if (this.mDataLv != null) {
                    this.mDataLv.post(new Runnable() {
                        public void run() {
                            LivePlayControlProgramsView.this.mDataLv.requestFocus();
                            LivePlayControlProgramsView.this.mDataLv.setSelection(clickedPosition);
                            LivePlayControlProgramsView.this.mDataLv.performItemClick(LivePlayControlProgramsView.this.mDataLv.getChildAt(clickedPosition), clickedPosition, (long) clickedPosition);
                            LogUtil.d(LivePlayControlProgramsView.TAG, "performItemClick=" + clickedPosition);
                        }
                    });
                }
            }
        }
    }

    public void setStateText(String text) {
        if (this.mBottomStateTv != null && !TextUtils.isEmpty(text)) {
            this.mBottomStateTv.setText(text);
        }
    }

    public List<LiveChannel> getDatas() {
        return this.mAdatper == null ? this.mDatas : this.mAdatper.getData();
    }

    public void setOnLiveProgramItemListener(OnLiveProgramItemListener onLiveProgramItemListener) {
        this.onLiveProgramItemListener = onLiveProgramItemListener;
    }

    public void notifyDataSetChanged() {
        if (this.mAdatper != null) {
            this.mAdatper.notifyDataSetChanged();
        }
    }

    public void setNetSpeed(String text) {
        if (this.mNetSpeedTv != null && !TextUtils.isEmpty(text)) {
            this.mNetSpeedTv.setText(text);
        }
    }

    public void onKeyDown(int keyCode) {
        if (keyCode == 19) {
            if (this.mDataLv.getSelectedItemPosition() == 0) {
                this.mDataLv.setSelection(this.mAdatper.getCount() - 1);
            }
        } else if (keyCode == 20 && this.mDataLv.getSelectedItemPosition() == this.mAdatper.getCount() - 1) {
            this.mDataLv.setSelection(0);
        }
        delayHide();
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (this.mDatas != null && this.mDatas.get(position) != null) {
            LiveChannel model = (LiveChannel) this.mDatas.get(position);
            this.mSequenceTv.setText((position + 1) + "");
            this.mChannelNameTv.setText(!TextUtils.isEmpty(model.getTitle()) ? model.getTitle() : "");
            if (this.onLiveProgramItemListener != null) {
                this.onLiveProgramItemListener.onLiveProgramItemClicked(parent, view, position, id);
                LogUtil.d(TAG, "onLiveProgramItemListener");
            }
            this.mAdatper.setClickedPosition(position);
            saveClickedRow(position);
        }
    }

    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
    }

    public void onNothingSelected(AdapterView<?> adapterView) {
    }

    public boolean onKey(View v, int keyCode, KeyEvent event) {
        LogUtil.d(TAG, "onkey");
        delayHide();
        return false;
    }

    public void changeProgram(int position) {
        final int index = position - 1;
        if (index >= 0 && index < this.mDatas.size() && this.mDataLv != null) {
            this.mDataLv.post(new Runnable() {
                public void run() {
                    LivePlayControlProgramsView.this.mDataLv.performItemClick(LivePlayControlProgramsView.this.mDataLv.getChildAt(index), index, (long) index);
                }
            });
        }
    }

    public void destory() {
        super.destory();
        stopLoadingAnimation();
    }
}
